package com.gxgeek.calculator.transfomer;

import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author gaoxing
 * @create 2018-06-04 下午7:58
 */
@Component
public class GroovyScriptTransformer implements ScriptTransformer{
    @Autowired
    Configuration freemarkerConfiguration;
    @Override
    public String transform(String className, String codeSegment) throws IOException, TemplateException {
        Map<String, Object> model = new HashMap();
        model.put("className", className);
        model.put("script", codeSegment);
        String text = FreeMarkerTemplateUtils.processTemplateIntoString( freemarkerConfiguration.getTemplate("groovyDataClass.ftl"),  model);
        return text;
    }
}
